{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Explaining the Loss of a Tree Model\n",
    "\n",
    "Explaining the loss of a model can be very useful for debugging and model monitoring. This notebook gives a very simple example of how this works. Note that explaining the loss of a model requires passing the labels, and is only supported for the `feature_perturbation=\"independent\"` option of TreeExplainer.\n",
    "\n",
    "This notebook will be fleshed out once we post a full write-up of this method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import xgboost\n",
    "\n",
    "import shap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Train an XGBoost Classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.43880873e-04, 2.47898608e-01, 1.17997164e-02, 7.11527169e-02,\n",
       "       6.41849875e-01, 1.76084566e+00, 5.70287136e-03, 8.60033274e-01,\n",
       "       4.78262809e-04, 6.43801317e-03])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X, y = shap.datasets.adult()\n",
    "\n",
    "model = xgboost.XGBClassifier()\n",
    "model.fit(X, y)\n",
    "\n",
    "# compute the logistic log-loss\n",
    "model_loss = -np.log(model.predict_proba(X)[:, 1]) * y + -np.log(model.predict_proba(X)[:, 0]) * (1 - y)\n",
    "\n",
    "model_loss[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Explain the Log-Loss of the Model with TreeExplainer\n",
    "\n",
    "Note that the `expected_value` of the model's loss depends on the label and so it is now a function instead of a single number."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.43887488e-04, 2.47898585e-01, 1.17997435e-02, 7.11527711e-02,\n",
       "       6.41849874e-01, 1.76084475e+00, 5.70285151e-03, 8.60033255e-01,\n",
       "       4.78233521e-04, 6.43796897e-03])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "explainer = shap.TreeExplainer(model, X, feature_perturbation=\"interventional\", model_output=\"log_loss\")\n",
    "explainer.shap_values(X.iloc[:10, :], y[:10]).sum(1) + np.array([explainer.expected_value(v) for v in y[:10]])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
